******************************************************************************************************		
***** write program to add different model statistics to stored estimates for multilevel models ******
******************** to show them in regression table ************************************************
******************************************************************************************************

capture program drop add_scalars_meologit
capture program drop add_scalars_mixed
capture program drop add_scalars_mixed_2level		
capture program drop add_scalars_mixed_3level		
		
				 program add_scalars_meologit
					matrix define groupN=e(N_g)
					scalar groupN=groupN[1,1]
					estadd scalar groupN = groupN
					estadd scalar ri_std = sqrt([/]_b[var(_cons[nuts3_2006])])
					capture estat icc
					capture scalar icc=r(icc2)
					capture estadd scalar icc=icc
					estat ic
					matrix define fitmatrix=r(S)
					scalar bic=fitmatrix[1,6]
					estadd scalar bic=bic
				end

				 program add_scalars_mixed
					matrix define groupN=e(N_g)
					scalar group1N=groupN[1,1]
					estadd scalar group1N = group1N
					estadd scalar ri1_std = exp([lns1_1_1]_b[_cons])
					capture estat icc
					capture scalar icc1=r(icc2)
					capture estadd scalar icc1=icc1
					estat ic
					matrix define fitmatrix=r(S)
					scalar bic=fitmatrix[1,6]
					estadd scalar bic=bic
				end

				program add_scalars_mixed_2level
					matrix define groupN=e(N_g)
					scalar group1N=groupN[1,1]
					scalar group2N=groupN[1,2]
					estadd scalar group1N = group1N
					estadd scalar group2N = group2N
					estadd scalar ri1_std = exp([lns1_1_1]_b[_cons])
					estadd scalar ri2_std = exp([lns2_1_1]_b[_cons])
					capture estat icc
					capture scalar icc1=r(icc3)
					capture scalar icc2=r(icc2)
					capture estadd scalar icc1=icc1
					capture estadd scalar icc2=icc2
					estat ic
					matrix define fitmatrix=r(S)
					scalar bic=fitmatrix[1,6]
					estadd scalar bic=bic
				end
	
			program add_scalars_mixed_3level
					matrix define groupN=e(N_g)
					scalar group1N=groupN[1,1]
					scalar group2N=groupN[1,2]
					scalar group3N=groupN[1,3]
					estadd scalar group1N = group1N
					estadd scalar group2N = group2N
					estadd scalar group3N = group3N
					estadd scalar ri1_std = exp([lns1_1_1]_b[_cons])
					estadd scalar ri2_std = exp([lns2_1_1]_b[_cons])
					estadd scalar ri3_std = exp([lns3_1_1]_b[_cons])
					capture estat icc
					capture scalar icc1=r(icc4)
					capture scalar icc2=r(icc3)
					capture scalar icc3=r(icc2)
					capture estadd scalar icc1=icc1
					capture estadd scalar icc2=icc2
					capture estadd scalar icc3=icc3
					estat ic
					matrix define fitmatrix=r(S)
					scalar bic=fitmatrix[1,6]
					estadd scalar bic=bic
				end